using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Portal;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;

namespace NCS.Module.CMS.Admin
{
    public partial class MVSettings : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dropMoViePortal.DataSource = PortalController.GetPortalsByLang(AppEnv.GetLanguage());
                dropMoViePortal.DataBind();
            }
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            dropHotNewsMovies.Items.Clear();
            dropMovieEvent.Items.Clear();
            dropMovieTinmoi.Items.Clear();
            dropcauchuyenhautruong.Items.Clear();
            drophosodienanh.Items.Clear();
            dropGallery.Items.Clear();
            dropBonus.Items.Clear();

            //trang cap 2 va chi tiet
            dropMVTinMoi.Items.Clear();
            dropMV_TinMoi_Position1.Items.Clear();
            dropMV_TinMoi_Position2.Items.Clear();

            dropMVCCHT.Items.Clear();
            dropMV_CCHT_Position1.Items.Clear();
            dropMV_CCHT_Position2.Items.Clear();

            dropMVHSDA.Items.Clear();
            dropMV_HSDA_Position1.Items.Clear();
            dropMV_HSDA_Position2.Items.Clear();

            dropMVBonus.Items.Clear();
            dropMV_Bonus_Position1.Items.Clear();
            dropMV_Bonus_Position2.Items.Clear();

            dropMVGallery.Items.Clear();
            dropMV_Gallery_Position1.Items.Clear();
            dropMV_Gallery_Position2.Items.Clear();

            
            int pmv = ConvertUtility.ToInt32(Portal[Constants.CMS_PortalMovies + AppEnv.GetLanguage()]);
            if (pmv > 0)
            {
                MiscUtility.SelectItemFromList(dropMoViePortal, pmv.ToString());
            }
            else
            {
                MiscUtility.SelectItemFromList(dropMoViePortal, ConvertUtility.ToString(Request.QueryString["portalid"]));
            }
            //movies
            ZoneController.LoadZones(dropMovieTinmoi.Items, dropMoViePortal.SelectedValue);
            foreach (ListItem item in dropMovieTinmoi.Items)
            {
                dropMovieEvent.Items.Add(new ListItem(item.Text, item.Value));
                dropHotNewsMovies.Items.Add(new ListItem(item.Text, item.Value));
                drophosodienanh.Items.Add(new ListItem(item.Text, item.Value));
                dropcauchuyenhautruong.Items.Add(new ListItem(item.Text, item.Value));
                dropGallery.Items.Add(new ListItem(item.Text, item.Value));
                dropBonus.Items.Add(new ListItem(item.Text, item.Value));

                //trang cap 2 va chi tiet
                dropMVTinMoi.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_TinMoi_Position1.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_TinMoi_Position2.Items.Add(new ListItem(item.Text, item.Value));

                dropMVCCHT.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_CCHT_Position1.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_CCHT_Position2.Items.Add(new ListItem(item.Text, item.Value));

                dropMVHSDA.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_HSDA_Position1.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_HSDA_Position2.Items.Add(new ListItem(item.Text, item.Value));

                dropMVBonus.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_Bonus_Position1.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_Bonus_Position2.Items.Add(new ListItem(item.Text, item.Value));

                dropMVGallery.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_Gallery_Position1.Items.Add(new ListItem(item.Text, item.Value));
                dropMV_Gallery_Position2.Items.Add(new ListItem(item.Text, item.Value));
            }

            int mv_tinmoi = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Tinmoi + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMovieTinmoi, mv_tinmoi.ToString());
            
            int mv_event = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Event + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMovieEvent, mv_event.ToString());
            
            int mv_cauchuyenht = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Cauchuyenhautruong + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropcauchuyenhautruong, mv_cauchuyenht.ToString());
            
            int mv_hosodienanh = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Hosodienanh + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(drophosodienanh, mv_hosodienanh.ToString());
            
            int mv_hotnews = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_HotNews + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropHotNewsMovies, mv_hotnews.ToString());

            int gallery = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Gallery + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropGallery, gallery.ToString());

            int bonus = ConvertUtility.ToInt32(Portal[Constants.CMS_MV_Bonus + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropBonus, bonus.ToString());
            
            //trang cap 2 va chi tiet

            // tin moi
            int Level2_TinMoi = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_MV_TinMoi + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMVTinMoi, Level2_TinMoi.ToString());

            int CMS_Level2_TinMoi_Position1 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + Level2_TinMoi + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_TinMoi_Position1, CMS_Level2_TinMoi_Position1.ToString());

            int CMS_Level2_TinMoi_Position2 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position2 + Level2_TinMoi + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_TinMoi_Position2, CMS_Level2_TinMoi_Position2.ToString());

            //ccht
            int Level2_CCHT = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_MV_CCHT + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMVCCHT, Level2_CCHT.ToString());

            int CMS_Level2_TinMoiNhan_Position1 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + Level2_CCHT + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_CCHT_Position1, CMS_Level2_TinMoiNhan_Position1.ToString());

            int CMS_Level2_TinMoiNhan_Position2 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position2 + Level2_CCHT + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_CCHT_Position2, CMS_Level2_TinMoiNhan_Position2.ToString());

            //hsda
            int CMS_Level2_HSDA = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_MV_HSDA + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMVHSDA, CMS_Level2_HSDA.ToString());

            int CMS_Level2_HSDA_Position1 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + CMS_Level2_HSDA + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_HSDA_Position1, CMS_Level2_HSDA_Position1.ToString());

            int CMS_Level2_HSDA_Position2 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position2 + CMS_Level2_HSDA + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_HSDA_Position2, CMS_Level2_HSDA_Position2.ToString());

            //gallery
            int CMS_Level2_Gallery = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_MV_Gallery + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMVGallery, CMS_Level2_Gallery.ToString());

            int CMS_Level2_Gallery_Position1 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + CMS_Level2_Gallery + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_Gallery_Position1, CMS_Level2_Gallery_Position1.ToString());

            int CMS_Level2_Gallery_Position2 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position2 + CMS_Level2_Gallery + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_Gallery_Position2, CMS_Level2_Gallery_Position2.ToString());

            //bonus
            int CMS_Level2_Bonus = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_MV_Bonus + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMVBonus, CMS_Level2_Bonus.ToString());

            int CMS_Level2_Bonus_Position1 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position1 + CMS_Level2_Bonus + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_Bonus_Position1, CMS_Level2_Bonus_Position1.ToString());

            int CMS_Level2_Bonus_Position2 = ConvertUtility.ToInt32(Portal[Constants.CMS_Level2_Position2 + CMS_Level2_Bonus + "_" + AppEnv.GetLanguage()]);
            MiscUtility.SelectItemFromList(dropMV_Bonus_Position2, CMS_Level2_Bonus_Position2.ToString());

        }

        protected void btnMoviesUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                PortalSetting.SetValue(Constants.CMS_PortalMovies + AppEnv.GetLanguage(), dropMoViePortal.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Tinmoi + AppEnv.GetLanguage(), dropMovieTinmoi.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Event + AppEnv.GetLanguage(), dropMovieEvent.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Hosodienanh + AppEnv.GetLanguage(), drophosodienanh.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Cauchuyenhautruong + AppEnv.GetLanguage(), dropcauchuyenhautruong.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_HotNews + AppEnv.GetLanguage(), dropHotNewsMovies.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Gallery+ AppEnv.GetLanguage(), dropGallery.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_MV_Bonus + AppEnv.GetLanguage(), dropBonus.SelectedValue);


                //trang cap 2 va chi tiet
                PortalSetting.SetValue(Constants.CMS_Level2_MV_TinMoi + AppEnv.GetLanguage(), dropMVTinMoi.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position1 + dropMVTinMoi.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_TinMoi_Position1.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position2 + dropMVTinMoi.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_TinMoi_Position2.SelectedValue);

                PortalSetting.SetValue(Constants.CMS_Level2_MV_CCHT + AppEnv.GetLanguage(), dropMVCCHT.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position1 + dropMVCCHT.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_CCHT_Position1.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position2 + dropMVCCHT.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_CCHT_Position2.SelectedValue);

                PortalSetting.SetValue(Constants.CMS_Level2_MV_HSDA + AppEnv.GetLanguage(), dropMVHSDA.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position1 + dropMVHSDA.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_HSDA_Position1.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position2 + dropMVHSDA.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_HSDA_Position2.SelectedValue);

                PortalSetting.SetValue(Constants.CMS_Level2_MV_Gallery + AppEnv.GetLanguage(), dropMVGallery.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position1 + dropMVGallery.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_Gallery_Position1.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position2 + dropMVGallery.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_Gallery_Position2.SelectedValue);

                PortalSetting.SetValue(Constants.CMS_Level2_MV_Bonus + AppEnv.GetLanguage(), dropMVBonus.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position1 + dropMVBonus.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_Bonus_Position1.SelectedValue);
                PortalSetting.SetValue(Constants.CMS_Level2_Position2 + dropMVBonus.SelectedValue + "_" + AppEnv.GetLanguage(), dropMV_Bonus_Position2.SelectedValue);
                
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

    }
}